package com.mysteel.cyx.demo.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Date;

import static cn.hutool.core.date.DatePattern.PURE_DATE_PATTERN;

/**
 * @Author：cyx
 * @Date：2024/4/2 9:03
 */
public final class DateUtils {


    /**
     * 获取指定时间戳偏移多少天的最后时间
     *
     * @param offset
     * @return
     */
    public static Long endTimeOfOffsetDay(Long time, int offset) {
        Date date = new Date(time);
        DateTime dateTime = DateUtil.offsetDay(date, offset);
        DateTime endOfOffsetDay = DateUtil.endOfDay(dateTime);
        return endOfOffsetDay.getTime();
    }

    /**
     * 获取指定时间戳偏移多少天的最后时间
     *
     * @param offset
     * @return
     */
    public static Long startTimeOfOffsetDay(Long time, int offset) {
        Date date = new Date(time);
        DateTime dateTime = DateUtil.offsetDay(date, offset);
        DateTime endOfOffsetDay = DateUtil.beginOfDay(dateTime);
        return endOfOffsetDay.getTime();
    }

    /**
     * 获取指定时间偏移多少天的开始时间
     *
     * @param day
     * @return
     */
    public static Long startTimeOfOffsetDay(int day, int offset) {
        DateTime dateTime = DateUtil.parse(String.valueOf(day), PURE_DATE_PATTERN);
        DateTime offsetDateTime = DateUtil.offsetDay(dateTime, offset);
        DateTime endOfOffsetDay = DateUtil.beginOfDay(offsetDateTime);
        return endOfOffsetDay.getTime();
    }


    /**
     * 获取指定时间偏移多少天的最后时间
     *
     * @param day
     * @return
     */
    public static Long endTimeOfOffsetDay(int day, int offset) {
        DateTime dateTime = DateUtil.parse(String.valueOf(day), PURE_DATE_PATTERN);
        DateTime offsetDateTime = DateUtil.offsetDay(dateTime, offset);
        DateTime endOfOffsetDay = DateUtil.endOfDay(offsetDateTime);
        return endOfOffsetDay.getTime();
    }

    /**
     * 格式化日期部分（不包括时间） 格式 yyyyMMdd
     */
    public static int getDimDay(Long time) {
        String formatDate = DateUtil.format(new Date(time), PURE_DATE_PATTERN);
        return Integer.parseInt(formatDate);
    }

    /**
     * 获取当天天的日期: 格式 yyyyMMdd
     */
    public static int currentDate() {
        String formatDate = cn.hutool.core.date.DateUtil.format(new Date(), PURE_DATE_PATTERN);
        return Integer.parseInt(formatDate);
    }


    /**
     * 两个日期相差天数
     */
    public static long betweenDay(Date beginDate, Date endDate) {
        return cn.hutool.core.date.DateUtil.betweenDay(beginDate, endDate, true);
    }

    public static void main(String[] args) {
        String registerChannel ="/www.mysteel.com";
        if (registerChannel.startsWith("/")){
            registerChannel= StrUtil.removePrefix(registerChannel, "/");
            registerChannel = StrUtil.prependIfMissing(registerChannel,"https://");
        }
       System.out.println(registerChannel);
    }
}
